package main

import (
	"crypto/ecdsa"
	"fmt"
	"gobcos/src/accounts/abi/bind"
	"gobcos/src/client"
	"gobcos/src/common"
	"gobcos/src/common/hexutil"
	"gobcos/src/crypto"
	"gobcos/src/fiscotest/helloworld"
	"log"
)

//创建外部账户，记得保存这些信息！！！私钥不可泄露
func main1() {
	//创建私钥
	privateKey, err := crypto.GenerateKey()
	if err != nil {
		log.Fatal(err)
	}
	privateKeyBytes := crypto.FromECDSA(privateKey)
	fmt.Println("private key: ", hexutil.Encode(privateKeyBytes)[2:]) // privateKey in hex without "0x"
	//创建公钥
	publicKey := privateKey.Public()
	publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
	if !ok {
		log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
	}

	publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
	fmt.Println("publick key: ", hexutil.Encode(publicKeyBytes)[4:]) // publicKey in hex without "0x"
	//创建地址
	address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
	fmt.Println("address: ", address) // account address

}

//get方法的调用
func main2() {
	//grouid = 1
	grouid := uint(1)
	//1.连接到rpc端口（连接到fisco）
	cli, err := client.Dial("http://106.55.171.201:8545", grouid)
	if err != nil {
		log.Panicln("连接失败", err)
	}
	defer cli.Close()
	//构造合约实例：合约地址 +连接
	//此处地址是填已发布的这个合约的地址
	//因为要建立这个合约的实例，所以导入test文件夹，直接 文件夹名.New文件名即可。
	ins, err := helloworld.NewHelloworld(common.HexToAddress("0x2d47f571731801903e7a8d09068caf479b274bad"), cli)
	if err != nil {
		log.Panicln("new World fail", err)
	}
	//生成调用函数CallOpts
	opts := bind.CallOpts{
		//这个是你自己的地址，告诉fisco，是谁调了
		From: common.HexToAddress("0xe1beB5919ff03eCA1359A8FC913Bf5a274819872"),
	}

	//调用get方法，返回结果和错误。
	re, err := ins.Get(&opts)
	if err != nil {
		log.Panicln(" get method fail", err)
	}
	//get得到的值
	fmt.Println(re)

}
